package com.softfuxi.tbs.app.utils;

import android.content.Context;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

/**
 * AUTHOR : 龚万全
 * TODO : Log工具类
 * DATE : 2016/12/19
 * VERSION : 1.0
 */
public class LocationTools {

    public AMapLocationClient mLocationClient = null;
    public AMapLocationClientOption mLocationOption = null;

    private OnLocationResultListener mOnLocationResultListener;

    public LocationTools(Context context) {
        try {
            //初始化定位
            if (mLocationClient == null) {
                mLocationClient = new AMapLocationClient(context);
            }
            if (mLocationOption == null) {
                mLocationOption = new AMapLocationClientOption();
                //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
                mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
                //缓存机制默认开启，可以通过以下接口进行关闭。
                //当开启定位缓存功能，在高精度模式和低功耗模式下进行的网络定位结果均会生成本地缓存，不区分单次定位还是连续定位。GPS定位结果不会被缓存。
                mLocationOption.setLocationCacheEnable(false);
                //获取最近3s内精度最高的一次定位结果：
                //设置setOnceLocationLatest(boolean b)接口为true，启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true，setOnceLocation(boolean b)接口也会被设置为true，反之不会，默认为false。
                mLocationOption.setOnceLocationLatest(true);
            }
        } catch (Exception e) {
            e.toString();
        }
    }

    //定位回调，interval定位间隔 多少毫秒内返回
    public void start(OnLocationResultListener onLocationResultListener) {
        //官方默认是2秒给结果
        start(onLocationResultListener, 2000);
    }


    public void start(OnLocationResultListener onLocationResultListener, int interval) {
        if(mLocationClient !=null){
            mLocationOption.setInterval(interval);
            this.mOnLocationResultListener = onLocationResultListener;
            //设置定位回调监听
            mLocationClient.setLocationListener(new AMapLocationListener() {
                @Override
                public void onLocationChanged(AMapLocation aMapLocation) {

                    if (aMapLocation != null) {
                        if (aMapLocation.getErrorCode() == 0) {

                            if (mOnLocationResultListener != null) {
                                mOnLocationResultListener.onLocationSuccess(aMapLocation);
                            }
                        } else {
                            if (mOnLocationResultListener != null) {
                                mOnLocationResultListener.onLocationFailed();
                            }
                        }
                        closeLocationClient();
                    }
                }
            });

            //给定位客户端对象设置定位参数
            mLocationClient.setLocationOption(mLocationOption);
            //启动定位
            mLocationClient.startLocation();
        }
    }

    private void closeLocationClient() {

        mLocationClient.stopLocation();
        mLocationClient = null;
        mOnLocationResultListener = null;
    }

    public interface OnLocationResultListener {
        void onLocationSuccess(AMapLocation aMapLocation);

        void onLocationFailed();
    }
}
